package Methods;

import SeqList.SeqList;
import Stack.Stack;
import TripleTable.TripleTable;

public class SearchAll {
    TripleTable data;
    int[][] compromise;//实在没有办法了……
    int count;//记录二维数组中的行数

    public int[][] searchAll(int id1, int id2) {//查询两景点间所有路径
        Stack path = new Stack(this.data.length());
        SeqList arr = new SeqList(this.data.length());
        initialize();
        path.push(id1);
        search( id1, path, arr, 0, id2);
        return this.compromise;
    }

    public void search(int from, Stack path, SeqList arr, int curLen, int end){
        int flag = 0;
        for (int i = 0; i < this.data.length(); i++) {
            if (this.data.getRow(i) == (int) from){//判断该结点是否有通往未访问的结点的路径
                if (this.data.getCol(i) == end){//判断路径是否抵达终点
                    for (int j = 0; j < curLen; j++) {//储存路径
                        this.compromise[this.count][j] = (int)arr.get(j);
                        this.count++;
                    }
                    path.popTop();
                    curLen--;
                    return;
                }
                path.push(this.data.getCol(i));
                arr.set(curLen, this.data.getCol(i));
                curLen++;
                flag = 1;
                search((int)path.getTop(), path, arr, curLen, end);
            }
        }
        if (flag == 0){//判断查询是否有结果
            path.popTop();
            curLen--;
            return;
        }
    }

    public void initialize(){//初始化二维数组
        for (int i = 0; i < this.data.length(); i++) {
            for (int j = 0; j < this.data.length(); j++) {
                this.compromise[i][j] = 0;
            }
        }
        this.count = 0;
    }
}
